package com.game.main;

import com.game.model.*;
import com.game.util.ApplicationUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: tjc
 * @description
 * @Date Created in 22:06 2021-11-30
 */
public class ModuleFactory {
    private static ModuleFactory instance;
    private ApplicationUtil beanContext;
    private MapContext mapContext;

    private ModuleFactory() {
    }

    public static synchronized ModuleFactory getInstance(String mapPath) {
        if (instance == null) {
            instance = new ModuleFactory();
            instance.beanContext = new ApplicationUtil(mapPath);
            instance.mapContext = (MapContext) instance.beanContext.getBean("context");
        }
        return instance;
    }

    //不安全，多次调用重复生成模型
    public List<Border> buildBorders() {
        List<Border> borderList = new ArrayList<>();
        int borderNum = mapContext.getBorder();
        for (int i = 0; i < borderNum; i++) {
            Border b = (Border) beanContext.getBean("border" + i);
            borderList.add(b);
        }
        return borderList;
    }

    public List<BirthPlace> buildBirthPlaces() {
        List<BirthPlace> list = new ArrayList<>();

        int birthPlaceNum = mapContext.getBirthPlace();
        for (int i = 0; i < birthPlaceNum; i++) {
            BirthPlace birthPlace = (BirthPlace) beanContext.getBean("birthPlace" + i);
            list.add(birthPlace);
        }
        return list;
    }

    public List<Wall> buildWallList() {
        List<Wall> list = new ArrayList<>();
        int wallNum = mapContext.getWall();
        for (int i = 0; i < wallNum; i++) {
            Wall wall = (Wall) beanContext.getBean("wall" + i);
            list.add(wall);
        }
        return list;
    }

    public List<Hero> buildHeros() {
        List<Hero> list = new ArrayList<>();
        Hero hero = (Hero) beanContext.getBean("hero");
        list.add(hero);
        return list;
    }

    public Home buildHome() {
        return (Home) beanContext.getBean("home");
    }

}
